#include "emfcolorscaledrawable.h"
#include "drawable.h"

EmfColorScaleDrawable::EmfColorScaleDrawable()
{
    _height = 0;
    _width = 0;
}

void EmfColorScaleDrawable::setColorScale(ColorScale *colorScale)
{
    _colorScale = colorScale;
}

void EmfColorScaleDrawable::setBoundingBox(QRectF boundingBox)
{
    _boundingBox = boundingBox;
    _boundingBox.setLeft(_boundingBox.left()-1000);
}




void EmfColorScaleDrawable::_drawText(QGLWidget *textRenderer)
{
    QColor color = QColor(125, 125, 125);    
    QFont scaleFont("Tahoma", 8);
    glColor4f(color.redF(),color.greenF(), color.blueF(),color.alphaF());    

    double dh = _height / 4;
    double dv = (_colorScale->getMaxValue() -_colorScale->getMinValue())/4;
    for(int i = 0; i <=4; i++)
    {
        double y = _boundingBox.top() + dh * i;
        QString label = QString::number(_colorScale->getMinValue() + dv*i);

        textRenderer->renderText(_boundingBox.left() + _width + 10 * label.length(), y, 0, label, scaleFont);
    }
}

void EmfColorScaleDrawable::draw(QGLWidget *textRenderer)
{
    _height = _boundingBox.height()*0.25;
    _width = _boundingBox.width()*0.05;
    double dh = fabs(_colorScale->getMaxValue() - _colorScale->getMinValue())/_height;

    glBegin(GL_QUADS);
    for(int i = 0; i < _height; i++)
    {
        QColor color = _colorScale->getColor(_colorScale->getMinValue() + dh*i);
        glColor4f(color.redF(),color.greenF(), color.blueF(),color.alphaF());

        glVertex2d(_boundingBox.left(), _boundingBox.top() + i);
        glVertex2d(_boundingBox.left() + _width, _boundingBox.top() + i);
        glVertex2d(_boundingBox.left() + _width, _boundingBox.top() + i+1);
        glVertex2d(_boundingBox.left(), _boundingBox.top() + i+1);
    }
    glEnd();
    _drawText(textRenderer);
}
